import { generateUUID } from '../../utils'
import { Port, PortType } from './canvasType'
import Canvas from './canvas'
import { Node } from './canvasType'

//根据用户选择的StylePort,获取对应的 Port
export function getPortByStylePort(stylePort: (PortType | undefined)[]) {
  const ports = [] as Port[]

  const initPort = (position: string, type: string | undefined, id: string | undefined) => {
    if (!type) return
    const portId = id || generateUUID()
    if (type === 'input') {
      ports.push({
        id: portId,
        group: position + 'In'
      })
    } else if (type === 'output') {
      ports.push({
        id: portId,
        group: position + 'Out'
      })
    } else if (type === 'twoway') {
      ports.push({
        id: portId,
        group: position + 'Twoway'
      })
    } else {
      return
    }
  }

  const positionData = ['top', 'bottom', 'left', 'right']
  for (let i = 0; i < positionData.length; i++) {
    initPort(positionData[i], stylePort[i]?.type, stylePort[i]?.id)
  }

  return ports
}

//记录历史行为
export function recordHistoryAction(
  action:
    | 'addNodes'
    | 'addEdges'
    | 'deleteNodes'
    | 'deleteEdges'
    | 'moveNodeRecord'
    | 'changeNodeSizeRecord',
  data: any[]
) {
  const history = Canvas.canvasHistory
  if (history.length > 50) {
    //删除头部的十个元素
    history.splice(0, 10)
  }
  if (action === 'addNodes' || action === 'addEdges') {
    history.push({
      action,
      data: (() => {
        const temp = data.map((item) => {
          return {
            id: item.id
          }
        })
        return temp
      })()
    })
  } else if (action === 'deleteNodes' || action === 'deleteEdges') {
    history.push({
      action,
      data: (() => {
        const temp = data.map((item) => {
          return {
            id: item.id,
            other: item
          }
        })
        return temp
      })()
    })
  } else if (action === 'moveNodeRecord') {
    history.push({
      action,
      data: data
    })
  } else if (action === 'changeNodeSizeRecord') {
    history.push({
      action,
      data: data
    })
  }

  Canvas.canvasHistory = history
}

//解析历史行为
export function parseHistoryAction(): void {
  const history = Canvas.canvasHistory
  if (history.length === 0) return
  const lastAction = history.pop()
  Canvas.canvasHistory = history
  if (!lastAction) return

  if (lastAction.action === 'addNodes') {
    const ids = lastAction.data.map((item) => {
      return item.id
    })
    Canvas.executeMethod('deleteNodes', 'mainProcess', true, false, ids)
  } else if (lastAction.action === 'addEdges') {
    const ids = lastAction.data.map((item) => {
      return item.id
    })
    Canvas.executeMethod('deleteEdges', 'mainProcess', true, false, ids)
  } else if (lastAction.action === 'deleteNodes') {
    const nodes = lastAction.data.map((item) => {
      return item.other
    })
    Canvas.executeMethod('addNodes', 'mainProcess', true, false, nodes)
  } else if (lastAction.action === 'deleteEdges') {
    const edges = lastAction.data.map((item) => {
      return item.other
    })
    Canvas.executeMethod('addEdges', 'mainProcess', true, false, edges)
  } else if (lastAction.action === 'moveNodeRecord') {
    const resultData = [] as Node[]

    lastAction.data.forEach((item) => {
      const nodes = Canvas.getNodes('id', item.id)
      if (nodes) {
        const node = nodes[0]
        node.x = item.x ?? node.x
        node.y = item.y ?? node.y
        resultData.push(node)
      }
    })

    Canvas.executeMethod('updateNodes', 'mainProcess', true, false, resultData)
  } else if (lastAction.action === 'changeNodeSizeRecord') {
    const { id, width, height } = lastAction.data[0]
    if (!id || !width || !height) return
    Canvas.executeMethod('changeNodeSize', 'mainProcess', true, false, id, width, height)
  }
}
